﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using EARS.App_Code;

namespace EARS
{
    public partial class CreateEvent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Event evt = new Event();
            if (DateTime.ParseExact(txtStartDate.Text, "d/M/yyyy", null) >= DateTime.ParseExact(txtEndDate.Text, "d/M/yyyy", null))
            {
                lblPopUp.Text = "Error: Incorrect selection of date. Please try again.";
                mpe.Show();
            }
            else if (DateTime.Parse(ddlStartTime.SelectedValue) >= DateTime.Parse(ddlEndTime.SelectedValue))
            {
                lblPopUp.Text = "Error: Incorrect selection of time. Please try again.";
                mpe.Show();
            }
            else if (DateTime.ParseExact(txtDeadline.Text, "d/M/yyyy", null) <= DateTime.ParseExact(DateTime.Now.Date.ToShortDateString(), "d/M/yyyy", null))
            {
                lblPopUp.Text = "Error: Incorrect selection of deadline. Please try again.";
                mpe.Show();
            }
            else
            {
                int limit = 0;
                if(!rbtnCheck.Checked)
                {
                    limit = int.Parse(txtLimit.Text);
                }
                if (evt.checkTiming(ddlVenue.SelectedValue, DateTime.ParseExact(txtStartDate.Text, "d/M/yyyy", null), DateTime.ParseExact(txtEndDate.Text, "d/M/yyyy", null), DateTime.Parse(ddlStartTime.SelectedValue).ToShortTimeString(), DateTime.Parse(ddlEndTime.SelectedValue).ToShortTimeString()) == true)
                {
                    try
                    {
                        evt.CreateEvent(new Event(txtEventName.Text, ddlVenue.SelectedValue, txtDescription.Text, DateTime.ParseExact(txtStartDate.Text, "d/M/yyyy", null), DateTime.ParseExact(txtEndDate.Text, "d/M/yyyy", null), DateTime.Parse(ddlStartTime.SelectedValue).ToShortTimeString(), DateTime.Parse(ddlEndTime.SelectedValue).ToShortTimeString(), limit, DateTime.ParseExact(txtDeadline.Text, "d/M/yyyy", null), rbnlExclusiveRole.SelectedValue, rbnlExclusiveYear.SelectedValue, ddlCourse.SelectedValue, User.Identity.Name));
                        lblPopUp.Text = "Event Successfully Created!";
                        mpe.Show();
                    }
                    catch (SqlException ex)
                    {
                        lblPopUp.Text = "Error: Unable to create event. Event name might have already been used. Please check all input and try again.";
                        mpe.Show();
                    }
                }
                else
                {
                    lblPopUp.Text = "ERROR: Time slot for event has been taken up. Please try again with a different time period.";
                    mpe.Show();
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }

        protected void rbtnCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnCheck.Checked)
            {
                txtLimit.Enabled = false;
            }
            else
            {
                txtLimit.Enabled = true;
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            if (lblPopUp.Text.Equals("Event Successfully Created!"))
            {
                mpe.Hide();
                Response.Redirect("~/Home.aspx");
            }
            else
            {
                mpe.Hide();
            }
        }
    }
}